package com.cwf.quality.common.strategy.analyzer;

import com.cwf.quality.common.enums.DataAnalyzerEnum;
import com.cwf.quality.model.dto.QualityResultInfo;
import com.cwf.quality.model.dto.dataanalyze.param.AnalyzerParamInfo;
import com.cwf.quality.model.dto.dataanalyze.result.RowsAnalyzeResult;
import com.cwf.quality.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class RowsDataAnalyzer implements IDataAnalyzer {
    /**
     * 总条数分析
     *
     * @param analyzerParamInfo
     * @return
     */
    @Override
    public QualityResultInfo<List<RowsAnalyzeResult>> analyzeData(AnalyzerParamInfo analyzerParamInfo) {
        SqlUtils sqlUtils = analyzerParamInfo.getSqlUtils();
        List<RowsAnalyzeResult> rowsAnalyzeResultList = new ArrayList<>();

        List<String> columnNameList = analyzerParamInfo.getColumnNameList();
        for (String columnName : columnNameList) {
            // 生成空值校验的SQL
            String sql = DataAnalyzerEnum.ROWS_ANALYZE.getSql()
                    .replace("{table}", analyzerParamInfo.getTableName())
                    .replace("{column}", columnName);

            List<Map<String, Object>> mapList = sqlUtils.query4List(sql, 1);
            Long rows = (Long) mapList.get(0).get("rows");
            rowsAnalyzeResultList.add(RowsAnalyzeResult.builder().columnName(columnName).rows(rows).build());
        }

        QualityResultInfo qualityResultInfo = QualityResultInfo.builder()
                .code(0)
                .msg("成功")
                .result(rowsAnalyzeResultList).build();
        return qualityResultInfo;
    }
}
